using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using DG.Tweening;
using TrainSystem;
using UnityEngine;

public class GameEntry : MonoBehaviour
{
    public static GameEntry Instance;
    public Train Train;
    public TrackPointManager trackPointManager;
    public WheatManager wheatManager;
    public JoystickUI JoystickUI;
    public UITutorise uiTutorise;
    public UIGoldProgress UIGoldProgress;

    public int score = 0;

    [Header("火车升级")] public int LevelTrainSpendCount; //升级火车需要花费的钱币数量

    [Header("一阶段升级")]
    public GameObject townCenter1;
    public GameObject Track1;
    public GameObject Track2;
    public MouseTrigger mouseTrigger1;
    public MouseTrigger mouseTrigger4;

    [Header("二阶段升级")] 
    public GameObject townCenter2;
    public GameObject Track3;
    public MouseTrigger mouseTrigger2;

    [Header("三阶段升级")]
    public GameObject townCenter3;
    public GameObject Track4;
    public MouseTrigger mousetrigger3;

    private int LastWidth;
    private int LastHeight;

    void Awake()
    {
        Instance = this;
    }
    // Start is called before the first frame update
    void Start()
    {
        EventManager.Instance.Subscribe(LevelTown1Event.EventId,LevelTown1CallBack);
        EventManager.Instance.Subscribe(LevelTown2Event.EventId,LevelTown2CallBack);
        EventManager.Instance.Subscribe(LevelTrainEvent.EventId,LevelTrainCallBack);
        EventManager.Instance.Subscribe(LevelTown3Event.EventId,LevelTown3CallBack);

        LastWidth = Screen.width;
        LastHeight = Screen.height;
    }

    private void OnDestroy()
    {
        EventManager.Instance.UnSubscribe(LevelTown1Event.EventId,LevelTown1CallBack);
        EventManager.Instance.UnSubscribe(LevelTown2Event.EventId,LevelTown2CallBack);
        EventManager.Instance.UnSubscribe(LevelTrainEvent.EventId,LevelTrainCallBack);
        EventManager.Instance.UnSubscribe(LevelTown3Event.EventId,LevelTown3CallBack);
    }

    private void Update()
    {
        if (LastWidth != Screen.width || LastHeight != Screen.height)
        {
            Debug.Log("111");
            ScreenChangeEvent screenChangeEvent = new ScreenChangeEvent();
            EventManager.Instance.Fire(this,screenChangeEvent);
            LastHeight = Screen.height;
            LastWidth = Screen.width;
        }
    }


    private void LevelTrainCallBack(object sender,EventArgs ea)
    {
        if (score < LevelTrainSpendCount || !Train.IsCanLevel)
        {
            return;
        }

        score -= LevelTrainSpendCount;
        LevelTrainCompleteEvent levelTrainCompleteEvent = new LevelTrainCompleteEvent();
        EventManager.Instance.Fire(this,levelTrainCompleteEvent);
        UIGoldProgress.RefreshProgress();
    }

    private void LevelTown1CallBack(object sender,EventArgs ea)
    {
        if (!Train.IsCanLevel || Train.IsCanLevelIndex != 1)
        {
            return;
        }
        
        Debug.LogError(222222);

        if (score >= mouseTrigger1.TargetScore - mouseTrigger1.CurrentScore) //花费会超出！
        {
            int UseScore = mouseTrigger1.TargetScore - mouseTrigger1.CurrentScore;
            score -= UseScore;
            mouseTrigger1.SetGold(() =>
            {
                mouseTrigger1.gameObject.SetActive(false);
                mouseTrigger4.gameObject.SetActive(true);
                mouseTrigger2.gameObject.SetActive(true);
                townCenter1.gameObject.SetActive(true);
                Track1.gameObject.SetActive(false);
                Track2.gameObject.SetActive(true);
                
                LevelTown1CompleteEvent levelTown1CompleteEvent = new LevelTown1CompleteEvent();
                EventManager.Instance.Fire(this,levelTown1CompleteEvent);
                UIGoldProgress.RefreshProgress();
            },UseScore);
        }
        else
        {
            int UseScore = score;
            score = 0;
            for (int i=0;i<UseScore;i++)
            {
                mouseTrigger1.SetGold(() =>
                {

                    mouseTrigger1.gameObject.SetActive(false);
                    mouseTrigger4.gameObject.SetActive(true);
                    mouseTrigger2.gameObject.SetActive(true);
                    townCenter1.gameObject.SetActive(true);
                    Track1.gameObject.SetActive(false);
                    Track2.gameObject.SetActive(true);
            
                    LevelTown1CompleteEvent levelTown1CompleteEvent = new LevelTown1CompleteEvent();
                    EventManager.Instance.Fire(this,levelTown1CompleteEvent);
                    UIGoldProgress.RefreshProgress();

                },UseScore);
            }
        }
    }

    private void LevelTown2CallBack(object sender,EventArgs ea)
    {
        if ( !Train.IsCanLevel || Train.IsCanLevelIndex != 2)
        {
            return;
        }
        
        if (score >= mouseTrigger2.TargetScore - mouseTrigger2.CurrentScore) //花费会超出！
        {
            int UseScore = mouseTrigger2.TargetScore - mouseTrigger2.CurrentScore;
            score -= UseScore;
            mouseTrigger2.SetGold(() =>
            {
                mouseTrigger2.gameObject.SetActive(false);
                mousetrigger3.gameObject.SetActive(true);
                townCenter2.gameObject.SetActive(true);
                Track1.gameObject.SetActive(false);
                Track2.gameObject.SetActive(false);
                Track3.gameObject.SetActive(true);

                LevelTown2CompleteEvent levelTown2CompleteEvent = new LevelTown2CompleteEvent();
                EventManager.Instance.Fire(this,levelTown2CompleteEvent);
                UIGoldProgress.RefreshProgress();
            },UseScore);
        }
        else
        {
            int UseScore = score;
            score = 0;
            for (int i=0;i<UseScore;i++)
            {
                mouseTrigger2.SetGold(() =>
                {

                    mouseTrigger2.gameObject.SetActive(false);
                    mousetrigger3.gameObject.SetActive(true);
                    townCenter2.gameObject.SetActive(true);
                    Track1.gameObject.SetActive(false);
                    Track2.gameObject.SetActive(false);
                    Track3.gameObject.SetActive(true);

                    LevelTown2CompleteEvent levelTown2CompleteEvent = new LevelTown2CompleteEvent();
                    EventManager.Instance.Fire(this,levelTown2CompleteEvent);
                    UIGoldProgress.RefreshProgress();

                },UseScore);
            }
        }
    }

    private async void LevelTown3CallBack(object sender,EventArgs ea)
    {
        if (!Train.IsCanLevel || Train.IsCanLevelIndex != 3)
        {
            return;
        }
        
        
        if (score >= mousetrigger3.TargetScore - mousetrigger3.CurrentScore) //花费会超出！
        {
            int UseScore = mouseTrigger2.TargetScore - mouseTrigger2.CurrentScore;
            score -= UseScore;
            mousetrigger3.SetGold(async () =>
            {
                mousetrigger3.gameObject.SetActive(false);
                townCenter3.gameObject.SetActive(true);
                Track1.gameObject.SetActive(false);
                Track2.gameObject.SetActive(false);
                Track3.gameObject.SetActive(false);
                Track4.gameObject.SetActive(true);

                LevelTown3CompleteEvent levelTown3CompleteEvent = new LevelTown3CompleteEvent();
                EventManager.Instance.Fire(this,levelTown3CompleteEvent);

                await Task.Delay(1500);
                VictoryEvent victoryEvent = new VictoryEvent();
                EventManager.Instance.Fire(this,victoryEvent);
                UIGoldProgress.RefreshProgress();
                GameEntry.Instance.JoystickUI.IsCanMove = false;
            },UseScore);
        }
        else
        {
            int UseScore = score;
            score = 0;
            for (int i=0;i<UseScore;i++)
            {
                mousetrigger3.SetGold(async() =>
                {

                    mousetrigger3.gameObject.SetActive(false);
                    townCenter3.gameObject.SetActive(true);
                    Track1.gameObject.SetActive(false);
                    Track2.gameObject.SetActive(false);
                    Track3.gameObject.SetActive(false);
                    Track4.gameObject.SetActive(true);

                    LevelTown3CompleteEvent levelTown3CompleteEvent = new LevelTown3CompleteEvent();
                    EventManager.Instance.Fire(this,levelTown3CompleteEvent);

                    await Task.Delay(1500);
                    VictoryEvent victoryEvent = new VictoryEvent();
                    EventManager.Instance.Fire(this,victoryEvent);
                    UIGoldProgress.RefreshProgress();
                    GameEntry.Instance.JoystickUI.IsCanMove = false;

                },UseScore);
            }
        }
        
    }


}
